﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DealTea(true);     
        }
        //en为true 加密 为false 解密
        private void DealTea(bool en)
        {
            string tmp;

            string skey = key.Text.Replace(" ", "");
            byte[] bitkey = new byte[skey.Length / 2];
            int t = 0;
            tmp = "";
            for (t = 0; t < bitkey.Length; t++)
            {

                bitkey[t] = Convert.ToByte(skey.Substring(t * 2, 2), 16);
                tmp = tmp + bitkey[t].ToString("x2") + " ";
            }
            tmp.Trim();
            key.Text = tmp.ToUpper();
            if (bitkey.Length != 16)
            {
                MessageBox.Show("密钥位数不正确");
                return;
            }

            string sinput = input.Text.Replace(" ", "").Replace("\r\n","");
            tmp = "";
            byte[] bitinput = new byte[sinput.Length / 2];
            t = 0;
            for (t = 0; t < bitinput.Length; t++)
            {
                try
                {
                    bitinput[t] = Convert.ToByte(sinput.Substring(t * 2, 2), 16);
                }
                catch(FormatException)
                {
                    MessageBox.Show("数据异常" + sinput.Substring(t * 2, 2) + t.ToString());
                    return;
                }
                tmp = tmp + bitinput[t].ToString("x2") + " ";
            }
            tmp.Trim();
            input.Text = tmp.ToUpper();
            byte[] bitoutput;
            if (en)
            {
               bitoutput = tea.Encrypt(bitinput, 0, bitinput.Length, bitkey);
            }
            else
            {
                bitoutput = tea.Decrypt(bitinput, 0, bitinput.Length, bitkey);
            }
            if(bitoutput == null)
            {
                MessageBox.Show("解密失败");
                return;
            }
            output.Text = "";
            foreach (byte b in bitoutput)
            {
                output.Text = output.Text + b.ToString("x2").ToUpper() + " ";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DealTea(false);
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (int i = 1; i < 20;i++ )
            {
                comboBox1.Items.Add(i.ToString());
            }
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MD5 md5Hasher = MD5.Create();
            byte[] data = Encoding.Default.GetBytes(textBox1.Text);
            for (int i = 0; i < Convert.ToInt16(comboBox1.Text); i++)
            {
                data = md5Hasher.ComputeHash(data);
            }

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            textBox2.Text =  sBuilder.ToString().ToUpper();
            if(totea.Checked)
            {
                key.Text = textBox2.Text;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string datainput = Dainput.Text.Replace(" ","");
            byte[] byteinput = new byte[datainput.Length/2];
            byte[] byteoutput = new byte[byteinput.Length];
            for (int i = 0; i < datainput.Length / 2;i++ )
            {
                byteinput[i] =Convert.ToByte(datainput.Substring(i * 2, 2),16);
                byteoutput[i] = (byte)~byteinput[i];
            }
            Daoutput.Text = "";
            foreach (byte b in byteoutput)
            {
                Daoutput.Text = Daoutput.Text + b.ToString("x2").ToUpper();
            }
        }

        private void textBox3_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void textBox3_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.All;
        }

        private void textBox3_DragDrop(object sender, DragEventArgs e)
        {
            string[] str = (string[])e.Data.GetData(DataFormats.FileDrop);
            foreach (string stri in str)
            {
                Console.WriteLine(stri);
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string input = tea16input.Text.Replace(" ","");
            byte[] btin = new byte[input.Length/2];
            for (int i = 0; i < btin.Length;i++ )
            {
                btin[i] =Convert.ToByte(input.Substring(i*2,2),16);
            }

            string key = tea16key.Text.Replace(" ","");
            byte[] bkey = new byte[key.Length/2];
            for (int i = 0; i < bkey.Length;i++ )
            {
                bkey[i] = Convert.ToByte(key.Substring(i*2,2),16);
            }
            byte[] boutput = new byte[8];
            tea.code(btin, 0, 0, boutput, 0, 0, bkey);
            tea16output.Text = "";
            foreach (byte b in boutput)
            {
                tea16output.Text = tea16output.Text + b.ToString("x2") + " ";
            }

        }

        private void button6_Click(object sender, EventArgs e)
        {
            string input = tea16input.Text.Replace(" ", "");
            byte[] btin = new byte[input.Length / 2];
            for (int i = 0; i < btin.Length; i++)
            {
                btin[i] = Convert.ToByte(input.Substring(i * 2, 2), 16);
            }

            string key = tea16key.Text.Replace(" ", "");
            byte[] bkey = new byte[key.Length / 2];
            for (int i = 0; i < bkey.Length; i++)
            {
                bkey[i] = Convert.ToByte(key.Substring(i * 2, 2), 16);
            }
            byte[] boutput = new byte[8];
            tea.decode(btin, 0, 0, boutput, 0, 0, bkey);
            tea16output.Text = "";
            foreach (byte b in boutput)
            {
                tea16output.Text = tea16output.Text + b.ToString("x2") + " ";
            }
        }

        private void AtoU_Click(object sender, EventArgs e)
        {
            int i = 0;
            string A = AtoUText.Text.Replace(" ","").Replace("\r\n","").Replace("\"","").Replace("\\x","").Replace("0x","");
            byte[] bA = new byte[A.Length/2];
            for (i = 0; i < A.Length / 2;i++ )
            {
                try
                {
                    bA[i] = Convert.ToByte(A.Substring(i * 2, 2),16);
                }
                catch (FormatException)
                {
                    MessageBox.Show("有不符合条件的字符" + A.Substring(i * 2, 2));
                }
                
            }
            AtoUText.Text = "";
            int len = 0;
             for (i = 0; i < bA.Length-1;i+=2 )
            {
                AtoUText.Text  = AtoUText.Text  + "\\u" + bA[i+1].ToString("x2") + bA[i].ToString("x2");
            }
            if (bA.Length%2 == 1)
            {
                AtoUText.Text = AtoUText.Text + "\\u90" + bA[i].ToString("x2");
            }

        }


    }
}
